package com.limou.service.impl;

import com.limou.mapper.ProductMapper;
import com.limou.mapper.ShoppingCartMapper;
import com.limou.pojo.Product;
import com.limou.pojo.ShoppingCart;
import com.limou.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public List<ShoppingCart> queryShoppingCartByUserId(Integer userId) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.queryShoppingCartByUserId(userId);
        System.out.println("========================================================");
        System.out.println("购物车操作前集合" + shoppingCarts);

        // 用productId为key，count为value
        Map<Integer, Integer> map = new HashMap<>();

        List<Product> productList = null;
        for (ShoppingCart shoppingCart : shoppingCarts) {
            // 商品id的集合
            productList = shoppingCart.getProductList();

            // 计算商品对应数量
            for (Product product : productList) {
                Integer productId = product.getId();

                if (map.containsKey(productId)) {
                    System.out.println("数量要自增啦！！！！！！！！！！！！！！！！");
                    Integer count = map.get(productId);
                    count++;
                    map.put(productId, count);
                } else {
                    map.put(productId, 1);
                }
            }

            for (Product product : productList) {
                System.out.println("准备改变");

                // 最后得到的map，key为product的id，count为对应的数量
                shoppingCart.setProductCount(map.get(product.getId()));
                System.out.println("改变数量中");

            }
        }

        System.out.println("========================================================");
        System.out.println("购物车还没改变金额集合" + shoppingCarts);

        // 总金额
        double totalProductPrice = 0.0;
        for (ShoppingCart shoppingCart : shoppingCarts) {
            totalProductPrice += shoppingCart.getProductPrice() * shoppingCart.getProductCount();
        }

        for (ShoppingCart shoppingCart : shoppingCarts) {
            if (shoppingCart.getTotalProductPrice() == null) {
                shoppingCart.setTotalProductPrice("");
            }
            shoppingCart.setTotalProductPrice(String.valueOf(totalProductPrice));
        }


        System.out.println("========================================================");
        System.out.println("购物车操作后集合" + shoppingCarts);

        return shoppingCarts;
    }

    @Override
    public void addProductToShoppingCart(Integer productId, Integer userId) {
        // 商品库存 - 1
        productMapper.subProductStock(productId);
        // 添加商品到购物车
        shoppingCartMapper.addProductToShoppingCart(productId, userId);

    }

    @Override
    public void deleteShoppingCartById(Integer shoppingCartId, Integer productId) {
        // 商品库存 + 1
        productMapper.addProductStock(productId);
        // 删除购物车中商品信息
        shoppingCartMapper.deleteShoppingCartById(shoppingCartId);
    }

    @Override
    public void deleteProductByUserId(Integer userId) {
        shoppingCartMapper.deleteProductByUserId(userId);
    }
}
